package com.huaao.micropolice.root.controllers.content;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huaao.micropolice.core.common.Context;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.model.content.Content;
import com.huaao.micropolice.core.model.system.User;
import com.huaao.micropolice.core.service.content.ContentService;
import com.huaao.micropolice.core.service.content.SubjectService;
import com.huaao.micropolice.root.controllers.BaseController;

@Controller
@RequestMapping("content")
public class ContentController extends BaseController{


	@Autowired
	private ContentService contentService;
	@Autowired
	private SubjectService subjectService;
	@RequestMapping("index")
	public String index(){
		
		return "content/contentIndex";
	}
	
	
	@RequestMapping("form")
	public String form(String contentId,HttpServletRequest reqeuest){
		Content content = new Content();
		if (StringUtils.isNotBlank(contentId)){
			content =  contentService.get(contentId);
		}else{
			String subjectId = reqeuest.getParameter("subjectId");
			if(StringUtils.isNotBlank(subjectId)){
				content.setSubject(subjectService.get(subjectId));
			}
		}
		
		request.setAttribute("content", content); 
		return "content/contentForm";
	}
	/**
	 * @return 内容分页列表
	 */
	@RequestMapping("listContent")
	@ResponseBody
	public ResponseEntity<Map<String, Object> > listContent(){
		Page page = getPage();
		List<Content> contentList = contentService.listContent(page);
		List<Map<String, Object>> respList = new ArrayList<>();
		for (Content content : contentList) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", content.getId());
			map.put("title", content.getTitle());
			map.put("content", content.getContent());
			map.put("summary", content.getSummary());
			map.put("summary_img", content.getSummary_img());
			map.put("status", content.getStatus());
			respList.add(map);
		}
		
		Map<String, Object> body = successMap();
		body.put("data", respList);
		Long recordsCount = contentService.getCount();
		body.put("records", recordsCount);
		body.put("total",Math.floor((double)recordsCount/page.getPageSize())+1);
		
		return ajaxModel(MediaType.APPLICATION_JSON, body);
	}
	
	@RequestMapping("listBySubjectId")
	@ResponseBody
	public ResponseEntity<Map<String, Object> > listBySubjectId(String subjectId){
		Page page = getPage();
		List<Content> contentList = contentService.listBySubjectId(subjectId,page);
		List<Map<String, Object>> respList = new ArrayList<>();
		for (Content content : contentList) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", content.getId());
			map.put("title", content.getTitle());
			map.put("content", content.getContent());
			map.put("summary", content.getSummary());
			map.put("summary_img", content.getSummary_img());
			map.put("status", content.getStatus());
			respList.add(map);
		}
		
		Map<String, Object> body = successMap();
		body.put("data", respList);
		Long recordsCount = contentService.getCountBySubjectId(subjectId);
		body.put("records", recordsCount);
		body.put("total",Math.floor((double)recordsCount/page.getPageSize())+1);
		return ajaxModel(MediaType.APPLICATION_JSON, body);
	}
	
	
	
	/**
	 * @param content 新添加内容
	 * @return
	 */
	@RequestMapping("saveContent")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> saveContent(Content content){
		if(StringUtils.isBlank(content.getId())){
			content.setId(null);
			content.setCreateTime(new Date());
			content.setCreator(Context.getCurrentUser());
			content.setLastUpdateTime(new Date());
			content.setUpdater(Context.getCurrentUser());
			
			String id = contentService.addContent(content);
		}else{
			content.setUpdater(Context.getCurrentUser());
			content.setLastUpdateTime(new Date());
			
			contentService.updateContent(content);
		}
		
		content = contentService.get(content.getId());
				
		Map<String, Object> map = new HashMap<>();
		map.put("id", content.getId());
		map.put("title", content.getTitle());
		map.put("content", content.getContent());
		map.put("summary", content.getSummary());
		map.put("summary_img", content.getSummary_img());
		map.put("status", content.getStatus());
		
		Map<String, Object> body = successMap();
		body.put("data", map);
		return ajaxModel(MediaType.APPLICATION_JSON, body);
		
	}
	
	
	@RequestMapping("deleteContent")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> deleteContent(String contentId){
		Assert.hasText(contentId, "内容ID不能为空");
		contentService.deleteContent(contentId);
		return ajaxModel(MediaType.APPLICATION_JSON, successMap());
	}
	
	


}
